module.exports = function(grunt) {
    'use strict';
    require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);
    grunt.loadNpmTasks("asset-cache-control");
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        //清除目录
        clean: {
            all: ['dist/**', 'dist/*.*'],
            // image: 'dist/img',
            // css: 'dist/css',
            // js: 'dist/js'
        },
        // 文件拷贝
        copy: {
            src: {
                files: [{
                    expand: true,
                    cwd: 'src/views/',
                    src: ['**/*.html'],
                    dest: 'WEB-INF/views'
                },
                {
                    expand: true,
                    cwd: 'src/admin/',
                    src: ['**'],
                    dest: 'WEB-INF/admin'
                }]
            },
            image: {
                files: [{
                    expand: true,
                    cwd: 'src/img/',
                    src: ['**/*.{png,jpg,jpeg,gif,ico}'],
                    dest: 'dist/img'
                }]
            },
            fonts: {
                files: [{
                    expand: true,
                    cwd: 'src/fonts/',
                    src: ['**'],
                    dest: 'dist/fonts'
                }]
            },
            css: {
                files: [{
                    expand: true,
                    flatten: true,
                    cwd: 'src/css/',
                    src: ['**'],
                    dest: 'dist/css'
                }]
            },
            js: {
                files: [{
                    expand: true,
                    cwd: 'src/js/',
                    src: ['**'],
                    dest: 'dist/js',
                    filter: "isFile"
                }]
            },
            lib: {
                files: [{
                    expand: true,
                    cwd: 'bower_components/',
                    src: [
                        '**/*.min.js'
                    ],
                    dest: 'lib/',
                    flatten: true,
                }]
            }
        },

        // 文件合并
        concat: {
            options: {
                separator: ';',
                stripBanners: true //去除代码中的注释
            },
            // 合并js
            jquery_plugin: {
                src: [
                    
                    "lib/jquery.easing.min.js",
                    "src/js/jq.hoverdir.js",
                    "src/js/jq.validate.js",
                    "src/js/snap.svg-min.js"
                    
                ],
                dest: "dist/js/jquery.plugin.js"
            },
            faxsun_main: {
                src: [
                    "lib/jquery.ua.js",
                    "src/js/jq.classie.js",
                    "src/js/faxsun.jq.tool.js",
                    "src/js/faxsun.jq.homepage.js",
                    "src/js/faxsun.jq.plugin.js"
                ],
                dest: "dist/js/faxsun.main.js"
            },
            faxsun_data: {
                src: [
                    "src/js/faxsun.jq.comment.js",
                    "src/js/faxsun.jq.follow.js",
                    "src/js/faxsun.jq.account.js",
                    "src/js/faxsun.jq.paging.js",
                    "src/js/faxsun.jq.index.js",
                    "src/js/faxsun.jq.stat.js",
                ],
                dest: "dist/js/faxsun.data.js"
            },
            faxsun_article: {
                src: [
                    "src/js/faxsun.jq.post.js",
                    "src/js/faxsun.jq.confirm.js",
                    "src/js/fingerprint.js"
                ],
                dest: "dist/js/faxsun.article.js"
            },
            ueditor: {
                src: [
                    "src/js/ueditor.config.js",
                    "src/js/ueditor.all.js",
                    "src/js/lang/zh-cn/zh-cn.js"
                ],
                dest: "dist/js/faxsun.ueditor.js"
            },
            // 合并css
            faxsun_info: {
                src: [
                    "src/css/faxsun-info.css"
                ],
                dest: "dist/css/faxsun-info.css"
            },
            faxsun_ie: {
                src: [
                    "src/css/faxsun-info-iehack.css"
                ],
                dest: "dist/css/faxsun-ie.css"
            },
            faxsun_mobile: {
                src: [
                    "src/css/swiper.min.css",
                    "src/css/faxsun-info-mobile.css"
                ],
                dest: "dist/css/faxsun-mobile.css"
            },
            faxsun_mobile_js: {
                src:[
                    "lib/jquery.easing.min.js",
                    "src/js/faxsun.jq.plugin.js",
                    "src/js/faxsun-for-mobile.js",
                    "src/js/third-party/swiper.min.js",
                    "src/js/faxsun.jq.account.js",
                    "src/js/fingerprint.js",
                    "src/js/faxsun.jq.stat.js"
                ],
                dest: "dist/js/faxsun.mobile.js"
            }
        },

        //压缩JS
        uglify: {
            prod: {
                options: {
                    mangle: {
                        except: ['require', 'exports', 'module', 'window']
                    },
                    compress: {
                        global_defs: {
                            PROD: true
                        },
                        dead_code: true,
                        pure_funcs: [
                            "console.log",
                            "console.info"
                        ]
                    }
                },

                /*files: [{
                    expand: true,
                    cwd: 'dist/js/',
                    src: [
                        'jquery.plugin.js',
                        'faxsun.main.js',
                        'faxsun.data.js',
                        'faxsun.article.js',
                        'faxsun.ueditor.js',
                        '!*.min.js'
                    ],
                    dest: 'dist',
                    ext: '.min.js'
                }]*/
                files: {
                    'dist/js/jquery.plugin.min.js': ['dist/js/jquery.plugin.js'],
                    'dist/js/faxsun.main.min.js': ['dist/js/faxsun.main.js'],
                    'dist/js/faxsun.data.min.js': ['dist/js/faxsun.data.js'],
                    'dist/js/faxsun.article.min.js': ['dist/js/faxsun.article.js'],
                    'dist/js/faxsun.ueditor.min.js': ['dist/js/faxsun.ueditor.js'],
                    'dist/js/faxsun.mobile.min.js': ['dist/js/faxsun.mobile.js'],

                    // 额外
                    'dist/js/jq.nivo.slider.min.js': ['dist/js/jq.nivo.slider.js'],
                    'dist/js/faxsun.jq.broadcast.min.js': ['dist/js/faxsun.jq.broadcast.js']
                }
            }
        },
        //压缩CSS
        cssmin: {
            prod: {
                options: {
                    report: 'gzip'
                },
                files: [{
                    expand: true,
                    cwd: 'dist',
                    src: ['css/*.css'],
                    dest: 'dist',
                    ext: '.min.css'
                }]
            }
        },
        // 检查js语法
        jshint: {
            files: ['src/**/*.js'],
            options: {
                "jshintrc": ".jshintrc"
            }
        },

        //压缩图片
        imagemin: {
            prod: {
                options: {
                    optimizationLevel: 7,
                    pngquant: true
                },
                files: [{
                    expand: true,
                    cwd: 'dist/',
                    src: ['img/**/*.{png,jpg,jpeg,gif,webp,svg,.png.tmp}'],
                    dest: 'dist'
                }]
            }
        },


        // 处理html中css、js 引入路径
        usemin: {
            html: ['WEB-INF/views/**/*.html'],
            css: ['dist/css/**/*.css'],
            js: ['dist/js/**/*.js'],
            options: {
                assetsDirs: [
                    'dist',
                    'dist/img',
                    'dist/css',
                    'dist/js'
                ],
                blockReplacements: {
                    css: function (block) {
                        return '<link rel="stylesheet" href="' + block.dest + '"/>';
                    }
                  }
            }
        },

        //压缩HTML
        htmlmin: {
            options: {
                removeComments: true, //删除HTML注释
                removeCommentsFromCDATA: false, //删除<script>和<style>标签内的HTML注释
                removeCDATASectionsFromCDATA: false, //删除<script>和<style>标签中的CDTA区段
                collapseWhitespace: true, //删除文档树中文本节点的空白
                collapseBooleanAttributes: true, //删除布尔属性
                removeAttributeQuotes: false, //删除属性的引号，当安全的情况下
                removeRedundantAttributes: true, //删除多余的属性，像type="text/javascript"
                useShortDoctype: true, //用短的HTML5的<!DOCTYPE html>代替doctype
                removeEmptyAttributes: true, //删除空（或空白）属性
                removeOptionalTags: false, //一些元素允许省略标签，像</td>
                removeEmptyElements: false //删除空元素
            },
            html: {
                files: [{
                    expand: true,
                    cwd: 'WEB-INF/',
                    src: ['views/**/*.html'],
                    dest: 'WEB-INF/'
                }]
            }
        },

        // 编译sass
        sass: {
            options: {
                sourcemap: 'auto',
                style: 'compact',
                update: true,
                noCache: true,
                debugInfo: false
            },
            dist: {
                files: [{
                    expand: true,
                    cwd: 'src/sass',
                    src: [
                        '*.scss',
                        '*.sass',
                        '*.css'
                    ],
                    dest: 'src/css',
                    ext: '.css'
                }]
            }
        },

        // 监视文件变化
        watch: {
            scripts: {
                files: [
                    'src/**/*.scss'
                ],
                tasks: ['sass']
            },
            client: {
                files: ['src/views/**/*.html', 'src/css/*', 'src/js/*', 'src/img/**/*'],
                options: {
                    livereload: true
                }
            }
        },

        // 添加时间戳
        cache: {
            // css
            info_css: {
                options: {},
                assetUrl: 'dist/css/faxsun-info.min.css',
                files: {
                    'tmp': ['WEB-INF/views/home/partials/head.html'],
                },
            },
            // js
            plugin_js: {
                options: {},
                assetUrl: 'dist/js/jquery.plugin.min.js',
                files: {
                    'tmp': ['WEB-INF/views/home/partials/script.html'],
                }
            },
            main_js: {
                options: {},
                assetUrl: 'dist/js/faxsun.main.min.js',
                files: {
                    'tmp': ['WEB-INF/views/home/partials/script.html'],
                }
            },
            data_js: {
                assetUrl: 'dist/js/faxsun.data.min.js',
                files: {
                    "tmp": ["WEB-INF/views/home/partials/script.html"]
                }
            },
            ueditor_js: {
                assetUrl: 'dist/js/faxsun.ueditor.min.js',
                files: {
                    "tmp": ["WEB-INF/views/home/partials/script.html"]
                }
            },
            article_js: {
                assetUrl: 'dist/js/faxsun.article.min.js',
                files: {
                    "tmp": ["WEB-INF/views/home/partials/script.html"]
                }
            },
            // mobile
            mobile_css: {
                assetUrl: 'dist/css/faxsun-mobile.min.css',
                files: {
                    "tmp": ["WEB-INF/views/mobile/partial/head.html"]
                }
            },
            mobile_js: {
                assetUrl: 'dist/js/faxsun.mobile.min.js',
                files: {
                    "tmp": ["WEB-INF/views/mobile/partial/script.html"]
                }
            }
        }
    });


    grunt.registerTask('build', [
        'clean',// 消除目标文件
        'copy', //复制文件
        'concat', //合并文件
        //'imagemin', //图片压缩
        'cssmin', //CSS压缩
        'uglify', //JS压缩
        // 'jshint', //js检查 （建议模块化开发启用 use strict）
        'usemin', //HTML处理
        'cache', //增加时间戳 消除用户缓存
        //'htmlmin' //HTML压缩
    ]);
    // 项目发布  消除目标文件夹 -> 复制文件 -> 合并文件 -> 压缩 -> 检查语法 -> html处理url定位 -> html压缩

    // 静态资源发布  消除目标文件夹 -> 复制文件 -> 合并文件 -> 压缩 -> 检查语法
    grunt.registerTask('s', [
        'copy', //复制文件
        'concat', //合并文件
        'imagemin', //图片压缩
        'cssmin', //CSS压缩
        'uglify', //JS压缩
        'jshint', //JS检查
    ]);

    // 纯文件拷贝
    // grunt.registerTask('copy', ['copy']);

    // 检查js语法
    grunt.registerTask('jsH', ['jshint']);

    // 编译sass -> css -> sass修改 ->监视修改后编译
    grunt.registerTask('sassW', ['sass', 'watch']);
    
    // css/js -> 合并css/js -> 压缩为css.min/js.min
    grunt.registerTask('min', ['concat', 'cssmin', 'uglify', 'jshint']);

};
